package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.ShiftBo;
import org.eiahe.hr.attendance.domain.vo.ShiftVo;
import org.eiahe.hr.attendance.service.IShiftService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 班次管理控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/shift")
public class ShiftController extends BaseController {

    private final IShiftService shiftService;

    /**
     * 查询班次列表
     */
    @SaCheckPermission("attendance:shift:list")
    @GetMapping("/list")
    public TableDataInfo<ShiftVo> list(ShiftBo bo, PageQuery pageQuery) {
        return shiftService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取班次详细信息
     */
    @SaCheckPermission("attendance:shift:query")
    @GetMapping("/{shiftId}")
    public R<ShiftVo> getInfo(@PathVariable Long shiftId) {
        return R.ok(shiftService.queryById(shiftId));
    }

    /**
     * 新增班次
     */
    @SaCheckPermission("attendance:shift:add")
    @Log(title = "班次管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ShiftBo bo) {
        return toAjax(shiftService.insertByBo(bo));
    }

    /**
     * 修改班次
     */
    @SaCheckPermission("attendance:shift:edit")
    @Log(title = "班次管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ShiftBo bo) {
        return toAjax(shiftService.updateByBo(bo));
    }

    /**
     * 删除班次
     */
    @SaCheckPermission("attendance:shift:remove")
    @Log(title = "班次管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shiftIds}")
    public R<Void> remove(@PathVariable Long[] shiftIds) {
        return toAjax(shiftService.deleteWithValidByIds(List.of(shiftIds)));
    }

    /**
     * 导出班次列表
     */
    @SaCheckPermission("attendance:shift:export")
    @Log(title = "班次管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ShiftBo bo, HttpServletResponse response) {
        List<ShiftVo> list = shiftService.queryList(bo);
        ExcelUtil.exportExcel(list, "班次数据", ShiftVo.class, response);
    }
}
